#include <string.h>
#include "InputElement.h"

InputElement::InputElement(TextureManager * textureManager,Vector2D position,float textSize, int numChars, int numLines)
:MenuElement(textureManager, "", position, Vector2D(textSize * numChars, textSize*1.5*numLines),NULL)
{
	this->numLines = numLines;
	this->numChars = numChars;
	this->textSize = textSize;
	textureIndex = -1;
	//label =  new PrintString(textureManager, "", position, textSize);
	textBox = new TextBox(textureManager,"", position, size, textSize);
}
InputElement::InputElement(TextureManager * textureManager,Vector2D position,float textSize, int numChars)
:MenuElement(textureManager, "", position, Vector2D(textSize * numChars, textSize*1.5),NULL)
{
	this->numLines = 1;
	this->numChars = numChars;
	this->textSize = textSize;
	textureIndex = -1;
	//label =  new PrintString(textureManager, "", position, textSize);
	textBox = new TextBox(textureManager,"", position, size, textSize);
}
InputElement::~InputElement()
{
	if(textBox)
	{
		delete(textBox);
		textBox = NULL;
	}
}
bool InputElement::HandleUserInput(unsigned char key, int modifier, bool isDepressed)
{
	printf("InputElement::HandleUserInput\n");
	if(hasFocus)
	{	 
		printf("he has focus!\n");
		char * str = textBox->GetText();
		char * newString;
		if((int)key == 8)
		{
			int length = 0;
			
			if(str)
				length = strlen(str);
				
			bsKeyIsDepressed = isDepressed;
			if(str && length > 0 && bsKeyIsDepressed && !bsKeyWasDepressed)
			{
				printf("backspace\n");
				//backspace
				printf("changing char = %c\n", str[length-1]);
				str[length-1] = '\0';
				textBox->SetText(str);
				free(str);
				str = NULL;
				printf("Here\n");
			}
			bsKeyWasDepressed = bsKeyIsDepressed;
		}
		else if((int)key >= (int)' ' && (int)key < 127)
		{
			if(!str)
			{
				newString = (char*)malloc(sizeof(char)*2);
				newString[0] = key;
				newString[1] = '\0';
				textBox->SetText(newString);
			}
			else if( strlen(str) < (numChars*numLines) && keyTable.HandleUserInput(key,isDepressed))
			{
			
				int length = strlen(str);
				newString = (char*)malloc(sizeof(char)*(length+2));
				strcpy(newString, str);
				newString[length] = key;
				newString[length+1] = '\0';
				textBox->SetText(newString);
				free(str);
				str = NULL;
			}
		}
		printf("leaving InputElement::HandleUserInput\n");
		return true;
	}
	printf("Leaving InputElement::HandleUserInput\n");
	return false;
}
void InputElement::Draw(Vector2D parentPosition)
{
	
	if(textBox)
	{
		textBox->Draw(parentPosition);
	}
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	glOrtho(0,100,100,0, -1, 1);
	
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();
	glTranslatef(0, 0, -0.5);
	
	
	glDisable(GL_TEXTURE_2D);
	
	
	
	Vector2D vertices[4];
	vertices[0] = parentPosition + position;
	vertices[1] = Vector2D(vertices[0].x + size.x, vertices[0].y);
	vertices[2] = Vector2D(vertices[1].x, vertices[1].y + size.y);
	vertices[3] = Vector2D(vertices[0].x, vertices[2].y);
	
	if(hasFocus)
	{
		glColor3f(1,1,0);
		glBegin(GL_LINE_LOOP);
		for(int i = 0; i < 4; i++)
		{
			glVertex3f(vertices[i].x, vertices[i].y, 1);
		}
		glEnd();
	}
	
	glColor3f(1,1,1);	
	glBegin(GL_QUADS);
	
	for(int i = 0; i < 4; i++)
	{
		glVertex3f(vertices[i].x, vertices[i].y, 1);
	}
	glEnd();
	
	glDisable(GL_TEXTURE_2D);
	glDisable(GL_BLEND);
	glPopMatrix();
	glDisable(GL_TEXTURE_2D);
	glDisable(GL_BLEND);
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
}
void InputElement::Clicked(Vector2D mousePosition)
{
	printf("Clicked an input element\n");
	printf("Focus = %d\n", hasFocus);
}
void InputElement::SetContent(char * content)
{
	if(textBox)
		textBox->SetText(content);
}
char * InputElement::GetContent()
{
	if(textBox)
		return textBox->GetText();
	return NULL;
}
